
var api_url = 'http://api.mxzhuawawa.com';

var index_url = "https://m.mxzhuawawa.com";

var login_url = "https://m.mxzhuawawa.com/wx_login.php"
var protocolStr = document.location.protocol;  
if(protocolStr == "http:")  
{  
    console.log("protocolStr:",protocolStr);
}  
else if(protocolStr == "https:")  
{  
    console.log("protocolStr:",protocolStr);
    var api_url = 'https://api.mxzhuawawa.com';

    var index_url = "https://m.mxzhuawawa.com";

    var login_url = "https://m.mxzhuawawa.com/wx_login.php"
}  
else  
{  
   console.log("other protocol");  
}  

//获取用户token
var token = getUserToken();
console.log(token)
var isLogin = true;


var user={};
var _num_=1;//最低金币可抓

if(!token) {
    isLogin = false;
}

if(getQueryString("recommend")){
    cookieSet("recommend",getQueryString("recommend"),100);
}

setVisit();

//记录访问次数
function setVisit() {
    if(cookieGet("uid") && (!cookieGet("visit_times"))) {
        fetchData("/OpenAPI/v1/Master/countUserInfo",{"case":"visit_times","uid":cookieGet("uid")},function(data){
            cookieSet("visit_times",1,24);
        })
    }
}

//记录访问次数
function setShare() {
    if(cookieGet("uid")) {
        fetchData("/OpenAPI/v1/Master/countUserInfo",{"case":"share_times","uid":cookieGet("uid")},function(data){
        })
    }
}

//jsonp 通用函数
function fetchData(url, data,callback) {
    var val = arguments[3]==false ? false : true;
    $.ajax({
        type:"post",
        url:api_url+url,
        jsonp:'callback',
        dataType:'jsonp',
        async:val,
        data:data,
        success:function(data){
            if(data.code == "401") {
                //用户没有登录
                logout();
            } else if(data.code == "0") {
                callback(data.data);
            } else {
                //错误信息
                console.log(data.msg);
            }
        }
    })
}


function cookieSet(name, value, hours) {
    var life = new Date().getTime();
    life += hours * 1000 * 60 * 60;
    var cookieStr = name + "=" + escape(value) + ";expires=" + new Date(life).toGMTString() + ";path=/;domain=.mxzhuawawa.com";
    document.cookie = cookieStr;
}

function cookieGet(name) {
    var cookies = document.cookie.split("; ");
    var i = 0;
    for (i = 0; i < cookies.length; i++) {
        var cookie2 = cookies[i].split("=");
        if (cookie2[0] == name) {
            return unescape(cookie2[1]);
        }
    }
    return '';
}

function cookieRemove(name) {
    var cookieStr = name + "=''" + escape('null') + ";expires=" + new Date().toGMTString() + ";path=/;domain=.mxzhuawawa.com";
    document.cookie = cookieStr;
}

//获取当前用户token
function getUserToken() {
    return cookieGet("token");
}

//判断是否为微信客户
function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

function isQQ(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/mqqbrowser|qzone|qqbrowser/i)){
        return true;
    }else{
        return false;
    }
}
var isqq=isQQ();
var iswx=isWeiXin()
//判断用户是否登录,未登录跳转到首页
function checkUserLogin() {
    if(!isLogin) {
        location.href = index_url;
    }
}

//用户退出登录
function logout() {
    location.href = "https://m.mxzhuawawa.com/logout.php";
}

//重新登录
function relogin() {
    cookieRemove('token')
    cookieRemove('nickname')
    cookieRemove('avatar')
    cookieRemove('coinbalance')
    cookieRemove('uid')

    location.href = login_url;
}



function addheader(title,url){
    var str=location.href;
    var ss='';
    if(str.indexOf("personal")<0&&str.indexOf("index")<0&&str.indexOf("html")>0){
        ss='<a href="'+url+'" class="back"></a>';
    }
    var html='<header class="fixed_top">'+ss+'<span>'+title+'</span></header>';
        $(".warp").before(html);
    // if(!iswx&&!isqq){
    //     var html='<header class="fixed_top">'+ss+'<span>'+title+'</span></header>';
    //     $(".warp").before(html);
    // }else{
    //     $(".warp").addClass("wx");
    // }
}

function isloginalert(){
    var html='<div class="isloginalert">'
                +'<div class="islogincontent">'
                +   '<p>请先登录</p>'
                +  '<a href="/wx_login.php" class="isloginsure">确定</a>'
                +'</div>'
            +'</div>';
    $(document.body).append(html);
}

$(document).on("click",".isloginalert",function(e){
    var target=$(e.target);
    if(!target.is(".islogincontent *")&&!target.is(".islogincontent")){
        $(".isloginalert").remove();
    }
})

$(document).on("click",".is_loginbtn",function(){
    if(!isLogin){
        isloginalert();
        event.preventDefault();
        return;
    }
})

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}


document.onreadystatechange = loadingChange;    
function loadingChange(){     
    if(document.readyState == "complete"){   
        $(".init_loading").hide();   
        $('.init_content').show();  
    }     
} 

function dropload(obj){
    $(obj).dropload({
        scrollArea: window,
        domUp : {
              domClass   : 'dropload-up',
              domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
              domUpdate  : '<div class="dropload-update">↑释放更新</div>',
              domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
          },
        domDown: {
            domClass: 'dropload-down',
            domRefresh: '<div class="dropload-refresh">上拉加载更多</div>',
            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
            domNoData: '<div class="dropload-noData">已无数据</div>'
        },
        loadUpFn : function(me){
            setTimeout(function(){
                window.location.reload();
                me.resetload();
            },1000)
        },
        loadDownFn: function (me) {
            setTimeout(function () {
                me.lock();
                me.noData();
                me.resetload();
                return;
            }, 500);
        }
    });
}

function isNewMessage(obj){
     $.ajax({
        type:'get',
        url:api_url+"/OpenAPI/v1/Message/isNewMessage",
        jsonp: 'callback',
        dataType:'jsonp',
        data:{token:token,page:1},
        success:function(data){
            console.log(data);
            if(data.data==1){
                $(obj).addClass("msg");
            }else{
                $(obj).removeClass("msg");
            }
        }
    })
}

